import { updateUserPassword } from '@/services/userService';
import { ModalForm, ProForm, ProFormText } from '@ant-design/pro-components';
import { message } from 'antd';

interface Props {
  modalVisible: boolean;
  userId: number;
  onCancel: () => void;
  onSubmit: () => void;
}

/**
 * 修改密码模态框
 * @param props
 * @constructor
 */
const UpdatePasswordModal = (props: Props) => {
  const { modalVisible, userId, onCancel, onSubmit } = props;

  return (
    <ModalForm
      title="修改密码"
      open={modalVisible}
      onFinish={async (values) => {
        const { oldPassword, newPassword, checkPassword } = values;
        try {
          await updateUserPassword({
            userId,
            oldPassword,
            newPassword,
            checkPassword,
          });
          message.success('修改成功');
          onSubmit();
          return true;
        } catch (e: any) {
          message.error('修改失败，' + e.message);
          return false;
        }
      }}
      onOpenChange={(visible) => {
        if (!visible) {
          onCancel();
        }
      }}
    >
      <ProFormText.Password
        name="oldPassword"
        label="原密码"
        rules={[{ required: true, message: '请输入原密码' }]}
      />
      <ProFormText.Password
        name="newPassword"
        label="新密码"
        rules={[{ required: true, message: '请输入新密码' }]}
      />
      <ProFormText.Password
        name="checkPassword"
        label="确认密码"
        rules={[{ required: true, message: '请再次输入新密码' }]}
      />
    </ModalForm>
  );
};

export default UpdatePasswordModal;
